package cm.tfd.hospital.ui.activity;

import android.annotation.TargetApi;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.tfd.hospital.R;
import com.wang.avi.AVLoadingIndicatorView;

import cm.tfd.hospital.util.SystemBarTintManager;

/**
 * Created by DELL on 2018/7/24.
 */

public abstract class BaseActivity extends AppCompatActivity implements View.OnClickListener {
    public SystemBarTintManager tintManager;

    protected TextView titleTv, tv_back;
    protected RelativeLayout moreImg;
    protected ImageView sayKeFu;
    protected Button titleLeftBtn, titleMidBtn;
    protected Button titleRightBtn;
    protected LinearLayout titleLeftLl;

    private Dialog mLoadingDialog;
    private View mLoadingView;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//            setTranslucentStatus(true);
//            tintManager = new SystemBarTintManager(this);
//            tintManager.setStatusBarTintEnabled(true);
//            tintManager.setStatusBarTintResource(R.color.status_bar);//通知栏所需颜色
//        }
        initLoadingView();
        initView(savedInstanceState);


        initData(savedInstanceState);
    }

    public void showLoading() {
        if (mLoadingDialog != null && !mLoadingDialog.isShowing() && !isFinishing()) {
            mLoadingDialog.show();
        }
    }


    public void dismissLoading() {
        if (mLoadingDialog != null && mLoadingDialog.isShowing()) {
            mLoadingDialog.dismiss();
        }
    }

    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }

    private void initLoadingView() {
        if (mLoadingDialog == null) {
            mLoadingView = View.inflate(this, R.layout.loading_layout, null);
            AVLoadingIndicatorView ivLoading = (AVLoadingIndicatorView) mLoadingView.findViewById(R.id.loading);
            ivLoading.show();
            mLoadingDialog = new Dialog(this, R.style.loadingDialog);
            mLoadingDialog.setContentView(mLoadingView);
            mLoadingDialog.setCancelable(true);
            mLoadingDialog.setCanceledOnTouchOutside(false);
        }
    }

    protected void initData(Bundle savedInstanceState) {
    }
    protected abstract void initView(Bundle savedInstanceState);

    public void OnMySelfClick(View v) {            //返回按钮
        titleLeftLl = (LinearLayout) findViewById(R.id.ll_title_left);
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (imm != null) {
            imm.hideSoftInputFromWindow(getWindow().getDecorView().getWindowToken(), 0);
            finish();
            overridePendingTransition(R.anim.push_right_out, R.anim.push_right_in);
        }

    }

    public void setRightBtn(String text) {//设置右上角按钮
        titleRightBtn = (Button) findViewById(R.id.btn_title_right);
        titleRightBtn.setText(text);
        titleRightBtn.setOnClickListener(this);
        titleRightBtn.setVisibility(View.VISIBLE);
    }

    public void setTitle(String title) {//设置主标题
        titleTv = (TextView) findViewById(R.id.tv_title);
        titleTv.setText(title);
    }
    /**
     * 设置标题
     * @param title
     */
    public void setTitleZ(String title){
        TextView titleTv = (TextView)findViewById(R.id.total_title);
        titleTv.setText(title);
    }

    public void hideBackLl() {//隐藏title按钮
        titleLeftLl = (LinearLayout) findViewById(R.id.ll_title_left);
        titleLeftLl.setVisibility(View.GONE);
    }

    public void hideBack() {//隐藏返回两个字
        tv_back = (TextView) findViewById(R.id.tv_back);
        tv_back.setVisibility(View.GONE);
    }

    public void setRightImage1() {
        sayKeFu = (ImageView) findViewById(R.id.say_kehu);
//        sayKeFu.setBackgroundResource(R.drawable.gengduo);
        moreImg.setVisibility(View.VISIBLE);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            finish();
            overridePendingTransition(R.anim.push_right_out, R.anim.push_right_in);
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }

    public void myIntent(Class<?> cls) {//跳转
        Intent intent = new Intent();
        intent.setClass(this, cls);
        intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
        startActivity(intent);
        overridePendingTransition(R.anim.push_left_in, R.anim.push_left_out);
    }

    public void hideBtn(String mode) {//隐藏title按钮
        titleRightBtn = (Button) findViewById(R.id.btn_title_right);
        titleLeftBtn = (Button) findViewById(R.id.btn_title_left);
        moreImg = (RelativeLayout) findViewById(R.id.img_more);
        if ("left".equals(mode)) {
            titleLeftBtn.setVisibility(View.GONE);
        } else if ("right".equals(mode)) {
            titleRightBtn.setVisibility(View.GONE);
            moreImg.setVisibility(View.GONE);
        } else if ("both".equals(mode)) {
            titleLeftBtn.setVisibility(View.GONE);
            titleRightBtn.setVisibility(View.GONE);
            moreImg.setVisibility(View.GONE);
        } else if ("more".equals(mode)) {
            moreImg.setVisibility(View.GONE);
        }
    }

    public void setMoreImg(int id) {//设置更多
        moreImg = (RelativeLayout) findViewById(R.id.img_more);
//        imageViews = (ImageView) findViewById(R.id.say_kehu);
//        imageViews.setImageResource(id);
        moreImg.setVisibility(View.VISIBLE);
        moreImg.setOnClickListener(this);
    }

    public void setSendMessage() {//设置发帖
        moreImg = (RelativeLayout) findViewById(R.id.img_more);
        sayKeFu = (ImageView) findViewById(R.id.say_kehu);
//        sayKeFu.setBackgroundResource(R.mipmap.qz_fabu);
        moreImg.setVisibility(View.VISIBLE);
        moreImg.setOnClickListener(this);
    }

    public void setLeftBtn(String text) {//设置左上角按钮
        titleLeftBtn = (Button) findViewById(R.id.btn_title_left);
        titleLeftBtn.setText(text);
        titleLeftBtn.setOnClickListener(this);
        titleLeftBtn.setVisibility(View.VISIBLE);
    }

    public void setMidBtn(String text) {//设置左上角按钮
        titleMidBtn = (Button) findViewById(R.id.btn_title_mid);
        titleMidBtn.setVisibility(View.GONE);
        titleMidBtn.setText(text);
        titleMidBtn.setOnClickListener(this);
        titleMidBtn.setVisibility(View.VISIBLE);
    }


    /**
     * 调节系统字体大小不影响app设置
     *
     * @return
     */
    @Override
    public Resources getResources() {
        Resources res = super.getResources();
        Configuration config = new Configuration();
        config.setToDefaults();
        res.updateConfiguration(config, res.getDisplayMetrics());
        return res;
    }

    @Override
    public void onClick(View v) {

    }

    /**
     * @Description:跳转
     * @Params:相应的页面
     */
    public void toPage(Class cls) {
        Intent intent = new Intent(this, cls);
        startActivity(intent);
    }
    /**
     * 设置右侧文字
     * @param text
     */
    public void setRightText(String text){
        TextView textView = (TextView)findViewById(R.id.text_more);
        textView.setText(text);
        textView.setTextSize(13);
        textView.setVisibility(View.VISIBLE);
    }


    /**
     * 设置软件盘的显示和隐藏
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        boolean flag = false;
        try {
            if (ev.getAction() == MotionEvent.ACTION_DOWN) {
                View v = getCurrentFocus();
                if (isShouldHideInput(v, ev)) {
                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    if (imm != null && v != null) {
                        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                    }
                }
            }
            flag = super.dispatchTouchEvent(ev);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return flag;
    }

    public boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && (v instanceof EditText)) {
            int[] leftTop = {0, 0};
            //获取输入框当前的location位置
            //获取输入框当前的location位置
            v.getLocationInWindow(leftTop);
            int left = leftTop[0];
            int top = leftTop[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right && event.getY() > top && event.getY() < bottom) {
                // 点击的是输入框区域，保留点击EditText的事件
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

    @Override
    protected void onDestroy() {

        if (mLoadingDialog != null) {
            if (mLoadingDialog.isShowing()) {
                mLoadingDialog.dismiss();
            }
            mLoadingDialog = null;
        }
        super.onDestroy();
    }
    public void showMsg(String msg){
        Toast.makeText(this,msg,Toast.LENGTH_SHORT).show();
    }
}
